/*
 * @Author: fanjs
 * @Date: 2025-03-05 17:53:05
 * @Description:
 */
import { request } from '../request';

/**
  * 获取邮箱配置列表 全量
  * @returns 邮箱配置列表
  */
export function fetchEmailListApi() {
  return request<any>({
    url: '/v1/email-config/list',
    method: 'get',
  });
}

/**
 * 创建 or 更新配置 => 验证
 * @param id? 配置ID
 * @param company => 163.com/126.com/qq.com/...
 * @param email_account *
 *
 * @param protocol => 协议类型 POP3/SMTP/IMAP
 * @param pass *
 * @returns
 */
export function createAndUpdateEmailApi(data: any) {
  return request<Api.Email.RegisterEmailConfigResponse>({
    url: '/v1/email-config/register',
    method: 'post',
    data
  });
}

/**
  * 校验链接状态
  * @returns 是否通过校验
  */
export function verifyStatusApi() {
  return request<boolean>({
    url: '/v1/email-config/verify',
    method: 'get',
  });
}

/**
 * 删除邮箱配置
 * @param email_id
 * @returns
 */
export function deleteConfigApi(email_id: string) {
  return request<boolean>({
    url: '/v1/email-config/delete/' + email_id,
    method: 'post',
  });
}

/**
 * 重新连接邮箱
 * @param email_id
 * @returns
 */
export function reconnectApi(email_id: string) {
  return request<boolean>({
    url: '/v1/email-config/reconnect/' + email_id,
    method: 'post',
  });
}

/**
 * 获取最后一次连接的配置信息
 * @returns 配置 | false
 */
export function fetchLatestConfigApi() {
  return request<any>({
    url: '/v1/email-config/latest',
    method: 'get',
  });
}

/**
 * 关闭链接
 * @returns
 */
export function closeApi() {
  return request<boolean>({
    url: '/v1/email-config/close',
    method: 'get',
  });
}

/**
 * 获取模板列表
 * @returns
 */
export function fetchTemplateListApi() {
  return request<any>({
    url: '/v1/email-template/list',
    method: 'get',
  });
}

/**
 * 获取模板详情
 * @param template_id
 * @returns
 */
export function fetchTemplateApi(template_id: string) {
  return request<any>({
    url: '/v1/email-template/' + template_id,
    method: 'get',
  });
}

/**
 * 创建模板
 * @param variables 变量
 * @param subject 主题
 * @param content 内容
 * @returns
 */
export function createTemplateApi(data: any) {
  return request<boolean>({
    url: '/v1/email-template/create',
    method: 'post',
    data
  });
}

/**
 * 更新模板
 * @param data
 * @returns
 */
export function updateTemplateApi(data: any) {
  return request<boolean>({
    url: '/v1/email-template/update',
    method: 'post',
    data
  });
}

/**
 * 删除模板
 * @param template_id
 * @returns
 */
export function deleteTemplateApi(template_id: string) {
  return request<boolean>({
    url: '/v1/email-template/' + template_id,
    method: 'delete',
  });
}


/**
 * 获取任务列表
 * @param data { email_id?: string }
 * @param page 页码
 * @param pageSize 条数
 * @returns
 */
export function fetchTaskListApi(data: any) {
  return request<any>({
    url: '/v1/email-task/list',
    method: 'post',
    data
  });
}

/**
 * 获取任务详情
 * @param task_id
 * @returns
 */
export function fetchTaskApi(task_id: string) {
  return request<any>({
    url: '/v1/email-task/' + task_id,
    method: 'get',
  });
}

/**
 * 创建任务
 * @param data { email_id: string, subject: string, content: string, variables: string[] }
 * @returns
 */
export function createTaskApi(data: any) {
  return request<boolean>({
    url: '/v1/email-task/create',
    method: 'post',
    data
  });
}

/**
 * 更新任务
 * @param data { task_id: string, subject: string, content: string, variables: string[] }
 * @returns
 */
export function updateTaskApi(data: any) {
  return request<boolean>({
    url: '/v1/email-task/update',
    method: 'post',
    data
  });
}

/**
 * 执行任务 - 开始发送
 * @param task_id 任务ID
 * @param data { email_id: string }
 * @returns
 */
export function sendEmailApi(task_id: string, data: any) {
  return request<any>({
    url: '/v1/email-task/send/' + task_id,
    method: 'post',
    data
  });
}



/**
 * 文件上传添加收件人
 * @param task_id
 * @returns 收件人list
 */
export function uploadRecipientApi(task_id: string) {
  return request<any>({
    url: '/v1/email-recipient/upload/' + task_id,
    method: 'post',
  });
}

/**
 * 获取收件人列表
 * @param data { task_id: string }
 * @param page 页码
 * @param pageSize 条数
 * @returns
 */
export function fetchRecipientListApi(data: any) {
  return request<any>({
    url: '/v1/email-recipient/list',
    method: 'post',
    data
  });
}

/**
 * 获取收件人列表 - 全量
 * @param task_id
 * @returns
 */
export function fetchRecipientListAllApi(task_id: string) {
  return request<any>({
    url: '/v1/email-recipient/list-all/' + task_id,
    method: 'get',
  });
}

/**
 * 删除收件人
 * @param data { recipient_id: string, task_id: string }
 * @returns
 */
export function deleteRecipientApi(data: any) {
  return request<boolean>({
    url: '/v1/email-recipient/delete',
    method: 'post',
    data
  });
}
